/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.basic.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.modules.basic.service.BasicAc02Service;
import me.zhengjie.modules.basic.domain.dto.BasicAc02QueryCriteria;
import lombok.RequiredArgsConstructor;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author ly
 * @date 2025-04-22
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "api")
@RequestMapping("/api/basicAc02")
public class BasicAc02Controller {

    private final BasicAc02Service basicAc02Service;
    private final SequenceService sequenceService;
    @Resource
    BusinessLogService businessLogService;

    @ApiOperation("人员参保信息导出")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('basicAc02:list')")
    public void exportBasicAc02(HttpServletResponse response, BasicAc02QueryCriteria criteria) throws IOException {
        basicAc02Service.download(basicAc02Service.queryAll(criteria), response);
    }

    @PostMapping("getAc02")
    public ResponseEntity<List<BasicAc02>> queryBasicAc02(@RequestBody BasicAc02 criteria) {

        return new ResponseEntity<>(basicAc02Service.getAc02(criteria),HttpStatus.OK);
    }

    @GetMapping
    @ApiOperation("人员参保信息查询")
    @PreAuthorize("@el.check('basicAc02:list')")
    public ResponseEntity<PageResult<BasicAc02>> queryBasicAc02(BasicAc02QueryCriteria criteria){
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(basicAc02Service.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("人员参保信息新增")
    @ApiOperation("人员参保信息新增")
    @PreAuthorize("@el.check('basicAc02:add')")
    public ResponseEntity<Object> createBasicAc02(@Validated @RequestBody BasicAc02 resources) throws IOException {
        Sequence sequence = new Sequence();
        sequence.setName("aaz159");
        resources.setAaz159(sequenceService.nextSequence(sequence)); //参保关系id;
        if (ValidateUtil.isEmpty(resources.getAac001())) {
            sequence.setName("aac001");
            resources.setAac001(sequenceService.nextSequence(sequence)); //个人编号;

        }
        // 查询同一个人是否在同一家单位新增过
        Map checkMap = new HashMap();
        checkMap.put("aac001", resources.getAac001());
        checkMap.put("aab001", resources.getAab001());
        checkMap.put("aae140", resources.getAae140());
        basicAc02Service.check(checkMap);

        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_RYCBXXXZ);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);
        // 保存
        basicAc02Service.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("人员参保信息修改")
    @ApiOperation("人员参保信息修改")
    @PreAuthorize("@el.check('basicAc02:edit')")
    public ResponseEntity<Object> updateBasicAc02(@Validated @RequestBody BasicAc02 resources) throws IOException {
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_RYCBXXBG);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);
        // 保存
        basicAc02Service.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("人员参保信息删除")
    @ApiOperation("人员参保信息删除")
    @PreAuthorize("@el.check('basicAc02:del')")
    public ResponseEntity<Object> deleteBasicAc02(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        basicAc02Service.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
